/**
 * Solr is an open source enterprise search server based on the Lucene Java search library, 
 * with XML/HTTP and JSON APIs, hit highlighting, faceted search, caching, replication, 
 * and a web administration interface. It runs in a Java servlet container such as Tomcat.
 */
package com.tianbing.adlottery.helper;

import java.util.List;

import com.fengniu.ejb.business.IContentBusiness;
import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Site;
import com.tianbing.adlottery.global.IConstants;

public class SolrIndexingWorker {

	private ISiteBusiness siteBusiness;

	private IContentBusiness contentBusiness;

	private ISiteDao siteDao;

	private IContentDao contentDao;

	int batchSize = 10;

	int maxCategoryMember = 100;

	public void setContentBusiness(IContentBusiness contentBusiness) {
		this.contentBusiness = contentBusiness;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	/**
	 * used by Spring Schedule Job Schedule job will rearrange the store's
	 * ranking membership of category
	 * 
	 */
	public void rankingStores() {
		Integer[] siteTypeIds = { IConstants.Type.SITE_STORE.getId() };
		List<Site> stores = siteDao.getListSiteBy(siteTypeIds, null, 0, batchSize, "s.lastRankedDate ASC");
		for (Site store : stores) {
			siteBusiness.rankingSite(store.getSysId(), maxCategoryMember);
		}
	}

	public void rankingWebSites() {
		Integer[] siteTypeIds = { IConstants.Type.SITE_WEBSITE.getId() };
		List<Site> stores = siteDao.getListSiteBy(siteTypeIds, null, 0, batchSize, "s.lastRankedDate ASC");
		for (Site store : stores) {
			siteBusiness.rankingSite(store.getSysId(), maxCategoryMember);
		}
	}

	public void rankingHordes() {
		Integer[] siteTypeIds = { IConstants.Type.SITE_HORDE.getId() };
		List<Site> stores = siteDao.getListSiteBy(siteTypeIds, null, 0, batchSize, "s.lastRankedDate ASC");
		for (Site store : stores) {
			siteBusiness.rankingSite(store.getSysId(), maxCategoryMember);
		}
	}

	public void rankingProducts() {
		Integer[] contentTypeIds = { IConstants.Type.CONTENT_PRODUCT.getId() };
		List<Content> contents = contentDao.getListContentBy(contentTypeIds, null, null, 0, batchSize,
				"c.lastRankedDate ASC");
		for (Content content : contents) {
			contentBusiness.rankingContent(content.getSysId(), maxCategoryMember);
		}
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setBatchSize(int batchSize) {
		this.batchSize = batchSize;
	}

	public void setMaxCategoryMember(int maxCategoryMember) {
		this.maxCategoryMember = maxCategoryMember;
	}
}
